package mainFrame;

import graphic.MyFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import mouseEvents.DrawFrame;
import Graph.Graph;

public class mainF{
	File Inced = new File("");
	File point = new File("");
	JFrame myframe;
	JFrame myframe1;
	JFrame drawframe;
	JFrame opt = new JFrame();
	private ListenerFactory factory = new ListenerFactory(); 
	JMenuBar menuBar = new JMenuBar();
	
	JFrame mainf =  new MainFrame();
	
	public mainF() throws FileNotFoundException{
		drawframe = new DrawFrame(new Graph(),1,5);
		menuBar.add( createFileMenu());
		menuBar.add(createGraph());
	}
	
	
public class MainFrame extends JFrame {
	 
	
	
	
	
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MainFrame() throws FileNotFoundException {
		super("Transport System");
		
	JLabel img = new JLabel(new ImageIcon("images.jpg"));
	
	setSize(1000, 700);
	
	
	setContentPane(new JScrollPane(img));
	menuBar.add( createFileMenu());
	menuBar.add(createGraph());
	menuBar.add(Box.createHorizontalGlue());
	JLabel icon = new JLabel(new ImageIcon("images/download.gif"));
	icon.setBorder(BorderFactory.createLoweredBevelBorder());
	menuBar.add(icon);
	
	setJMenuBar(menuBar);
	setVisible(true);
}

	
}


private JMenu createFileMenu() {
	JMenu file = new JMenu("File");
	JMenuItem open = new JMenuItem("Open Graph From Existing Files", new ImageIcon("images/open16.gif"));
	JMenuItem exit = new JMenuItem(new ExitAction());
	open.addActionListener(factory.readFile());
	file.add(open);
	
	file.addSeparator();
	file.add(exit);
	return file;
}

private JMenu createGraph(){
	JMenu newGraph = new JMenu("Graph Edit");
	
	JMenuItem open = new JMenuItem("Create New Graph", new ImageIcon("images/open16.gif"));
	JMenuItem line =  new JMenuItem("Open Last Edited Graph");
	
	open.addActionListener(factory.createGraph());
	newGraph.add(open);
	line.addActionListener(factory.drawG());
	newGraph.addSeparator();
	newGraph.add(line);
	
	return newGraph;
}



class ExitAction extends AbstractAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		ExitAction() {
			putValue(NAME, "Exit");
	}
		
	public void actionPerformed(ActionEvent e) {
		System.exit(0);
	}
}


class ListenerFactory {
	public ActionListener getButtonL() {
		return new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("ButtonListener");
			}};
	}
	public WindowListener getWindowL() {
		return new WindowL();
	}
	
class WindowL extends WindowAdapter {
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
}
	
	
	
	
	public ActionListener createGraph() {
		return new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			 mainf.setVisible(false);
			drawframe.setVisible(true);
		}};
	}
	public ActionListener drawG() {
		return new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			mainf.setVisible(false);
			
			try {
				myframe = new MyFrame(new Graph("Point_of_Graph", "Incedent"));
				myframe.setVisible(true);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
				mainf.setVisible(true);
				JOptionPane.showMessageDialog(
						myframe1, "No Created Graph!! Please, Create Graph");
			}
			
		}};
	}
	
	
	public ActionListener writeT() {
		return new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			 mainf.setVisible(false);
			try {
				myframe = new MyFrame(new Graph("Point_of_Graph", "Incedent"));
				myframe.setVisible(true);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
				
				JOptionPane.showMessageDialog(
						myframe1, "No Created Graph!! Please, Create Graph");
			}
			
		
		
		}};
	}
	
	
	public ActionListener drawGfromFile() {
		return new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			 mainf.setVisible(false);
			if(point!=null&& Inced !=null ){
			try {
				myframe1 = new MyFrame(new Graph(point.getAbsolutePath(), Inced.getAbsolutePath()));
				myframe1.setVisible(true);
				point = null;
				Inced = null;
			} catch (FileNotFoundException e1) {
				JOptionPane.showMessageDialog(
						myframe1, "No files!!!!");
						
				e1.printStackTrace();
			}}else{
				JOptionPane.showMessageDialog(
						myframe1, "No files!!!! Please Choose files!!!!");
				
			}
			
		
		
		}};
	}
	
	public ActionListener readFile() {
		return new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			@SuppressWarnings("unused")
			SimpleFileChooser c = new SimpleFileChooser();
		}};
		}
	}

public class SimpleFileChooser extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JFileChooser fc = new JFileChooser();
	public SimpleFileChooser() {
	super("Choose Files");
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	JButton open = new JButton("Open incedent");
	open.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	fc.setDialogTitle("Choose incedent file");
	fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
	
	fc.getFileView();
	
	int res = fc.showOpenDialog(SimpleFileChooser.this);
	if ( res == JFileChooser.APPROVE_OPTION ){
	JOptionPane.showMessageDialog(
	SimpleFileChooser.this, fc.getSelectedFile());
	File Inced1 =  fc.getSelectedFile();
	
	Inced = Inced1;}
	}});
	
	JButton openP = new JButton("Open point");
	openP.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	fc.setDialogTitle("Choose point file");
	fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
	fc.getFileView();
	
	int res = fc.showOpenDialog(SimpleFileChooser.this);
	if ( res == JFileChooser.APPROVE_OPTION ){
	JOptionPane.showMessageDialog(
	SimpleFileChooser.this, fc.getSelectedFile());
	File point1 =  fc.getSelectedFile();
	point = point1;}
	}});
	
	
	JButton make = new JButton("create");
	make.addActionListener(factory.drawGfromFile());
	
	JButton save = new JButton("Сохранить...");
	save.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	fc.setDialogTitle("Сохранение файла");
	fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
	int res = fc.showSaveDialog(SimpleFileChooser.this);
	if ( res == JFileChooser.APPROVE_OPTION )
	JOptionPane.showMessageDialog(
	SimpleFileChooser.this, "Файл сохранен");
	}});
	JPanel contents = new JPanel();
	contents.add(open);
	contents.add(openP);
	contents.add(make);
	setContentPane(contents);
	pack();
	setVisible(true);
	}
	}
}



